$(function() {
    $('body').inputAction({
        global: true
    });
    var emptyFunction = function() {};
    var tip = function(obj, info) {
        obj.tip({
            content: info,
            background: "#000",
            color: "#fff"
        });
    };
    // 初始化支付信息
    var payType_val = $('#uclbrtSwitchContainer');
    window.payInfoDetail = {};
    ajaxRequest('/Home/Hotel/payInfo', {
        hotel: window.currentHotel
    }, 'POST', function(data) {
        var payType = parseInt(data.data.type);
        var payTypeInfo = data.data.partnerPayInfo;
        var payUsable = data.data.payUsable;
        if(!payUsable.uclbrtAlipayEnable){
            $('#uclbrtAlipayEnable').data('ison',0);
        };
        if(!payUsable.uclbrtWechatEnable){
            $('#uclbrtWechatEnable').data('ison',0);
        };
        if(!payUsable.hotelAlipayEnable){
            $('#hotelAlipayEnable').data('ison',0);
        };
        if(!payUsable.hotelWechatEnable){
            $('#hotelWechatEnable').data('ison',0);
        };      
        if(payUsable.uclbrtAlipayEnable && (payType&1)>0){
            $('#uclbrtAlipayEnable').addClass('is-checked').find('input').attr('checked',true);
            $('#uclbrtAlipayLabel').html(lang('yi'));           
        }else{
            $('#uclbrtAlipayEnable').removeClass('is-checked').find('input').attr('checked',false);
            $('#uclbrtAlipayLabel').html(lang('wei'));
        };
        if(payUsable.uclbrtWechatEnable && (payType&2)>0){
            $('#uclbrtWechatEnable').addClass('is-checked').find('input').attr('checked',true);
            $('#uclbrtWechatLabel').html(lang('yi')); 
        }else{
            $('#uclbrtWechatEnable').removeClass('is-checked').find('input').attr('checked',false);
            $('#uclbrtWechatLabel').html(lang('wei'));
        };
        if(payUsable.hotelAlipayEnable && (payType&16)>0){
            $('#hotelAlipayEnable').addClass('is-checked').find('input').attr('checked',true);
            $('#hotelAlipayLabel').html(lang('yi')); 
        }else{
            $('#hotelAlipayEnable').removeClass('is-checked').find('input').attr('checked',false);
            $('#hotelAlipayLabel').html(lang('wei'));
        };
        if(payUsable.hotelWechatEnable && (payType&32)>0){
            $('#hotelWechatEnable').addClass('is-checked').find('input').attr('checked',true);
            $('#hotelWechatLabel').html(lang('yi')); 
        }else{
            $('#hotelWechatEnable').removeClass('is-checked').find('input').attr('checked',false);
            $('#hotelWechatLabel').html(lang('wei'));
        };
        if((payType&64)>0){
            $('#hotelVipcardEnable').addClass('is-checked').find('input').attr('checked',true);
            $('#hotelVipcardLabel').html(lang('yi')); 
        }else{
            $('#hotelVipcardEnable').removeClass('is-checked').find('input').attr('checked',false);
            $('#hotelVipcardLabel').html(lang('wei'));
        };
        if (payTypeInfo === undefined) {
            return;
        };
        window.payInfoDetail = payTypeInfo;
        if (payTypeInfo.wechat !== undefined && payTypeInfo.wechat.appid !== null && payTypeInfo.wechat.appid !== '') {
            $('#wechatPayInfo').html(lang('write'));
            $('#wechatPayAppId').val(payTypeInfo.wechat.appid);
            $('#wechatPayAppSecret').val(payTypeInfo.wechat.appsecret);
            $('#wechatPayMChID').val(payTypeInfo.wechat.mchid);
            $('#wechatPayMChKey').val(payTypeInfo.wechat.mchkey);
        }
        if (payTypeInfo.ali !== undefined && payTypeInfo.ali.account !== null && payTypeInfo.ali.account !== '') {
            $('#alipayInfo').html(lang('write'));
        }
    }, emptyFunction);


    // 畅联代收开关 支付宝
    $('#uclbrtAlipayEnable').on('click', function(e) {
        e.preventDefault();
        var me = $(this);
        if(parseInt(me.data('ison')) == 0){
            tip(me, lang('notFunction'));
            return false;
        };
        var isHotelAlipay= $('#hotelAlipayEnable').data('ison'); 
        var isUclbrtAlipay = $('#uclbrtAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isUclbrtWechat = $('#uclbrtWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltAlipay = $('#hotelAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltWechat = $('#hotelWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltVipcard = $('#hotelVipcardEnable').find('input[type=checkbox]').prop('checked');
        var type = 0;
        if(!isUclbrtAlipay){
            type = type|1;
        }
        if(isUclbrtWechat){
            type = type|2;
        }
        if(isHoteltAlipay && isUclbrtAlipay){
            type = type|16;
        }
        if(isHoteltWechat){
            type = type|32;
        }
        if(isHoteltVipcard){
            type = type|64;
        };
        var params = {
            hotel: window.currentHotel,
            type: type
        };  
        ajaxRequest('/Home/Hotel/togglePayType', params, 'post', function(data, submitBtn) {
            var domBtn = submitBtn;
            if (isUclbrtAlipay) {
                domBtn.removeClass('is-checked').find('input').prop('checked',false);
                $('#uclbrtAlipayLabel').html(lang('wei'));
            } else {
                domBtn.addClass('is-checked').find('input').prop('checked',true);
                $('#uclbrtAlipayLabel').html(lang('yi'));
                if(parseInt(isHotelAlipay) != 0){
                    $('#hotelAlipayEnable').removeClass('is-checked').find('input').prop('checked',false);
                    $('#hotelAlipayLabel').html(lang('wei'));
                };
            };
            tip(submitBtn, lang('successfullySet'));
        }, function(error, submitBtn, defaultErrorHandler) {
            if (error.responseJSON === undefined) {
                tip(submitBtn, lang('tip.system'));
            } else {
                tip(submitBtn, error.responseJSON.info);
            }
        }, me);         
    });
    // 畅联代收开关 微信
    $('#uclbrtWechatEnable').on('click', function(e) {
        e.preventDefault();
        var me = $(this);
        if(parseInt(me.data('ison')) == 0){
            tip(me, lang('notFunction'));
            return false;
        };
        var isHotelWechat= $('#hotelWechatEnable').data('ison'); 
        var isUclbrtAlipay = $('#uclbrtAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isUclbrtWechat = $('#uclbrtWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltAlipay = $('#hotelAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltWechat = $('#hotelWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltVipcard = $('#hotelVipcardEnable').find('input[type=checkbox]').prop('checked');
        var type = 0;
        if(isUclbrtAlipay){
            type = type|1;
        }
        if(!isUclbrtWechat){
            type = type|2;
        }
        if(isHoteltAlipay){
            type = type|16;
        }
        if(isHoteltWechat && isUclbrtWechat){
            type = type|32;
        }
        if(isHoteltVipcard){
            type = type|64;
        };   
        var params = {
            hotel: window.currentHotel,
            type: type
        };  
        ajaxRequest('/Home/Hotel/togglePayType', params, 'post', function(data, submitBtn) {
            var domBtn = submitBtn; 
            if (isUclbrtWechat) {
                domBtn.removeClass('is-checked').find('input').prop('checked',false);
                $('#uclbrtWechatLabel').html(lang('wei'));
            } else {
                domBtn.addClass('is-checked').find('input').prop('checked',true);
                $('#uclbrtWechatLabel').html(lang('yi'));
                if(!isUclbrtAlipay && !isHoteltAlipay){
                    modalAlert(lang('Wechat.pay'));
                }
                if(parseInt(isHotelWechat) != 0){
                    $('#hotelWechatEnable').removeClass('is-checked').find('input').prop('checked',false);
                    $('#hotelWechatLabel').html(lang('wei'));
                };
            };
            tip(submitBtn, lang('successfullySet'));
            
        }, function(error, submitBtn, defaultErrorHandler) {
            if (error.responseJSON === undefined) {
                tip(submitBtn, lang('tip.system'));
            } else {
                tip(submitBtn, error.responseJSON.info);
            }
        }, me); 
       
    });    
    // 商户支付 支付宝
    $('#hotelAlipayEnable').on('click', function(e) {
        e.preventDefault();
        var me = $(this);
        if(parseInt(me.data('ison')) == 0){
            tip(me, lang('notFunction'));
            return false;
        };
        var isHotelAlipay= $('#uclbrtAlipayEnable').data('ison');
        var isUclbrtAlipay = $('#uclbrtAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isUclbrtWechat = $('#uclbrtWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltAlipay = $('#hotelAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltWechat = $('#hotelWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltVipcard = $('#hotelVipcardEnable').find('input[type=checkbox]').prop('checked');
        var type = 0;
        if(isUclbrtAlipay && isHoteltAlipay){
            type = (type|1);
        }
        if(isUclbrtWechat){
            type = (type|2);
        }
        if(!isHoteltAlipay){
            type = (type|16);
        }
        if(isHoteltWechat){
            type = (type|32);
        }
        if(isHoteltVipcard){
            type = type|64;
        };      
        var params = {
            hotel: window.currentHotel,
            type: type 
        };  
        ajaxRequest('/Home/Hotel/togglePayType', params, 'post', function(data, submitBtn) {
            var domBtn = submitBtn; 
            if (isHoteltAlipay) {
                domBtn.removeClass('is-checked').find('input').prop('checked',false);
                $('#hotelAlipayLabel').html(lang('wei'));
            } else {
                domBtn.addClass('is-checked').find('input').prop('checked',true);
                $('#hotelAlipayLabel').html(lang('yi'));
                if(parseInt(isHotelAlipay) != 0){
                    $('#uclbrtAlipayEnable').removeClass('is-checked').find('input').prop('checked',false);
                    $('#uclbrtAlipayLabel').html(lang('wei'));
                };
            };
            tip(submitBtn, lang('successfullySet'));
        }, function(error, submitBtn, defaultErrorHandler) {
            if (error.responseJSON === undefined) {
                tip(submitBtn, lang('tip.system'));
            } else {
                tip(submitBtn, error.responseJSON.info);
            }
        }, me); 
    });
    // 商户支付 微信
    $('#hotelWechatEnable').on('click', function(e) {
        e.preventDefault();
        var me = $(this);
        if(parseInt(me.data('ison')) == 0){
            tip(me, lang('notFunction'));
            return false;
        };
        var isuclbrtWechat= $('#uclbrtWechatEnable').data('ison'); 
        var isUclbrtAlipay = $('#uclbrtAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isUclbrtWechat = $('#uclbrtWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltAlipay = $('#hotelAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltWechat = $('#hotelWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltVipcard = $('#hotelVipcardEnable').find('input[type=checkbox]').prop('checked');
        var type = 0;
        if(isUclbrtAlipay){
            type = type|1;
        }
        if(isUclbrtWechat && isHoteltWechat){
            type = type|2;
        }
        if(isHoteltAlipay){
            type = type|16;
        }
        if(!isHoteltWechat){
            type = type|32;
        }
        if(isHoteltVipcard){
            type = type|64;
        };
        var params = {
            hotel: window.currentHotel,
            type: type 
        };  
        ajaxRequest('/Home/Hotel/togglePayType', params, 'post', function(data, submitBtn) {
            var domBtn = submitBtn;
            if (isHoteltWechat) {
                domBtn.removeClass('is-checked').find('input').prop('checked',false);
                $('#hotelWechatLabel').html(lang('wei'));
            } else {
                domBtn.addClass('is-checked').find('input').prop('checked',true);
                $('#hotelWechatLabel').html(lang('yi'));
                if(!isUclbrtAlipay && !isHoteltAlipay){
                    modalAlert(lang('Wechat.pay'));
                }
                if(parseInt(isuclbrtWechat) != 0){
                    $('#uclbrtWechatEnable').removeClass('is-checked').find('input').prop('checked',false);
                    $('#uclbrtWechatLabel').html(lang('wei'));
                };
            };
            tip(submitBtn, lang('successfullySet'));
        }, function(error, submitBtn, defaultErrorHandler) {
            if (error.responseJSON === undefined) {
                tip(submitBtn, lang('tip.system'));
            } else {
                tip(submitBtn, error.responseJSON.info);
            }
        }, me); 
    });
    // 商户支付 会员卡
    $('#hotelVipcardEnable').on('click', function(e) {
        e.preventDefault();
        var me = $(this);
        if(parseInt(me.data('ison')) == 0){
            tip(me, lang('notFunction'));
            return false;
        };
        var isUclbrtAlipay = $('#uclbrtAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isUclbrtWechat = $('#uclbrtWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltAlipay = $('#hotelAlipayEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltWechat = $('#hotelWechatEnable').find('input[type=checkbox]').prop('checked');
        var isHoteltVipcard = $('#hotelVipcardEnable').find('input[type=checkbox]').prop('checked');
        var type = 0;
        if(isUclbrtAlipay){
            type = type|1;
        }
        if(isUclbrtWechat && isHoteltWechat){
            type = type|2;
        }
        if(isHoteltAlipay){
            type = type|16;
        }
        if(isHoteltWechat){
            type = type|32;
        }
        if(!isHoteltVipcard){
            type = type|64;
        };

        var params = {
            hotel: window.currentHotel,
            type: type 
        };
        ajaxRequest('/Home/Hotel/togglePayType', params, 'post', function(data, submitBtn) {
            var domBtn = submitBtn;
            if (isHoteltVipcard) {
                domBtn.removeClass('is-checked').find('input').prop('checked',false);
                $('#hotelVipcardLabel').html(lang('wei'));
            } else {
                domBtn.addClass('is-checked').find('input').prop('checked',true);
                $('#hotelVipcardLabel').html(lang('yi'));
            };
            tip(submitBtn, lang('successfullySet'));
        }, function(error, submitBtn, defaultErrorHandler) {
            if (error.responseJSON === undefined) {
                tip(submitBtn, lang('tip.system'));
            } else {
                tip(submitBtn, error.responseJSON.info);
            }
        }, me); 
    });        

    $('#alipayModel').on('show.bs.modal', function() {
        if (window.payInfoDetail.ali === undefined) {
            return;
        }
        $('#alipayAccount').val(window.payInfoDetail.ali.account);
        $('#alipayPId').val(window.payInfoDetail.ali.pid);
        $('#alipayKey').val(window.payInfoDetail.ali.key);
        formInlineRight($('#alipayAccount'));
        formInlineRight($('#alipayPId'));
        formInlineRight($('#alipayKey'));
        formInlineRight($('#submitAlipay'));
    });

    $('#alipayModel').inputValidate({
        submit: {
            query: '#submitAlipay'
        },
        inputs: [{
            query: '#alipayAccount',
            regexp: /.{0,50}/,
            message: lang('alipayAccount'),
            require: false
        }, {
            query: '#alipayPId',
            regexp: /.{0,40}/,
            message: lang('alipayPId'),
            require: false
        }, {
            query: '#alipayKey',
            regexp: /.{0,40}/,
            message: lang('alipayKey'),
            require: false
        }]
    });

    $('#submitAlipay').on('successSubmit.amos.inputValidate', function(e) {
        var submitBtn = $(this);
        var aliInfo = {
            account: $('#alipayAccount').val(),
            pid: $('#alipayPId').val(),
            key: $('#alipayKey').val()
        };
        ajaxRequest('/Home/Hotel/savePayInfo', {
            hotel: window.currentHotel,
            ali: aliInfo
        }, 'POST', function(data) {
            window.payInfoDetail.ali = aliInfo;
            $('#alipayInfo').html(lang('write'));
            $('#alipayModel').modal('hide');
        }, null, submitBtn);
    });

    $('#wechatPayModel').on('show.bs.modal', function() {
        if (window.payInfoDetail.wechat === undefined) {
            return;
        }
        $('#wechatPayAppId').val(window.payInfoDetail.wechat.appid);
        $('#wechatPayAppSecret').val(window.payInfoDetail.wechat.appsecret);
        $('#wechatPayMChID').val(window.payInfoDetail.wechat.mchid);
        $('#wechatPayMChKey').val(window.payInfoDetail.wechat.mchkey);
        $('#wechatMyVerify').val(window.payInfoDetail.wechat.mpverify);
        formInlineRight($('#wechatPayAppId'));
        formInlineRight($('#wechatPayAppSecret'));
        formInlineRight($('#wechatPayMChID'));
        formInlineRight($('#wechatPayMChKey'));
        formInlineRight($('#wechatMyVerify'));
        formInlineRight($('#submitWechatPay'));
    });

    $('#wechatPayModel').inputValidate({
        submit: {
            query: '#submitWechatPay'
        },
        inputs: [{
            query: '#wechatPayAppId',
            regexp: /.{0,32}/,
            message: lang('wechatPayAppId'),
            require: false
        }, {
            query: '#wechatPayAppSecret',
            regexp: /.{0,32}/,
            message: lang('wechatPayAppSecret'),
            require: false
        }, {
            query: '#wechatPayMChID',
            regexp: /.{0,32}/,
            message: lang('wechatPayMChID'),
            require: false
        }, {
            query: '#wechatPayMChKey',
            regexp: /.{0,50}/,
            message: lang('wechatPayMChKey'),
            require: false
        }]
    });

    $('#submitWechatPay').on('successSubmit.amos.inputValidate', function(e) {
        var submitBtn = $(this);
        var wechatInfo = {
            appid: $('#wechatPayAppId').val(),
            appsecret: $('#wechatPayAppSecret').val(),
            mchid: $('#wechatPayMChID').val(),
            mchkey: $('#wechatPayMChKey').val(),
            mpverify: $('#wechatMyVerify').val()
        };
        ajaxRequest('/Home/Hotel/savePayInfo', {
            hotel: window.currentHotel,
            wechat: wechatInfo
        }, 'POST', function(data) {
            window.payInfoDetail.wechat = wechatInfo;
            $('#wechatPayInfo').html(lang('write'));
            $('#wechatPayModel').modal('hide');
        }, null, submitBtn);
    });

});
